<template>
    <div class="dashboard-container">
    <!-- 基础题库 -->
    <!-- 搜索from表单 -->
      <div class="main">
        <div class="test-head">
          <div class="size">
            说明：目前支持学科和关键字条件筛选
          </div>
          <div>
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              @click="addTest"
            >
              新增试题
            </el-button>
          </div>
        </div>
      <el-form
        :model="questionSearch"
        label-width="100px"
        class="form-container"
        ref="searchForm"
      >
        <div>
          <el-form-item
            label="学科"
            prop="subjectID"
          >
            <el-select
              v-model="questionSearch.subjectID"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, i) in simpleList"
                :key="i"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="二级目录"
            prop="catalogID"
          >
            <el-select
              v-model="questionSearch.catalogID"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, i) in catalogueslist"
                :key="i"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="标签"
            prop="tags"
          >
            <el-select
              v-model="questionSearch.tags"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, i) in direction"
                :key="i"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="关键字"
            prop="keyword"
          >
          <el-input
            placeholder="根据题干搜索"
            v-model="questionSearch.keyword"
          />
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="试题类型"
            prop="questionType"
          >
            <el-select
              v-model="questionSearch.questionType"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, i) in questionTypes"
                :key="i"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="难度"
            prop="difficulty"
          >
            <el-select
              v-model="questionSearch.difficulty"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, i) in difficulties"
                :key="i"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="方向"
            prop="direction"
          >
            <el-select
              v-model="questionSearch.direction"
              placeholder="请选择"
            >
              <el-option
                v-for="(item, i) in direction"
                :key="i"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="录入人"
            prop="creatorID"
          >
            <div>
              <el-select
                v-model="questionSearch.creatorID"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, i) in usersList"
                  :key="i"
                  :label="item.username"
                  :value="item.id"
                />
              </el-select>
            </div>
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="题目备注"
            prop="remarks"
          >
          <el-input
            v-model="questionSearch.remarks"
          />
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="企业简称"
            prop="shortName"
          >
          <el-input
            v-model="questionSearch.shortName"
          />
          </el-form-item>
        </div>
        <div>
          <el-form-item
            label="城市:"
          >
            <div class="city-select">
              <div>
                <el-select
                  v-model="questionSearch.province"
                  placeholder="请选择"
                  @change="changeCity"
                >
                  <el-option
                    v-for="(item, i) in cityslist(citydatas)"
                    :key="i"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </div>
              <div>
                <el-select
                  v-model="questionSearch.city"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, i) in district(questionSearch.province,citydatas)"
                    :key="i"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </div>
            </div>
          </el-form-item>
        </div>
        <div class="bnt">
          <el-button
            size="small"
            @click="eliminate"
          >
            清除
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="enterpriseSearch"
          >
            搜索
          </el-button>
        </div>
      </el-form>

      <!-- 记录 -->
      <div class="record-container">
        <i class="el-icon-info"></i>
        共 {{ basicquestions.total }} 条记录
      </div>
      <!-- 表单 -->
      <div class="derive-list">
        <my-table
          :table-props="{
            data: basicquestions.items,
          }"

          :columns="[
            { label: '试题编号', prop: 'number', width: 120, align: 'center' },
            { label: '学科', prop: 'subject', align: 'center' },
            { label: '目录', prop: 'catalog', align: 'center'},
            { label: '题型', prop: 'questionType', align: 'center'},
            { label: '题干', slotName: 'question', align: 'center', width: 180},
            { label: '录入时间', prop: 'addDate', align: 'center', width: 180},
            { label: '难度', prop: 'difficulty', align: 'center'},
            { label: '录入人', prop: 'creator', align: 'center' },
            { label: '操作', slotName: 'operators', align: 'center', width: 200},
          ]"
          :pagination-props="{
            background: true,
            layout: 'prev, pager, next, sizes, jumper',
            total: basicquestions.total,
            pageSize: 5,
            pageSizes: [5, 10, 20, 50],
          }"
          :pagination-events="events"
        >
          <template
            v-slot:operators="scope"
          >
            <div class="operation">
              <div title="预览">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-view"
                  circle
                  @click="preview(scope.row.id )"
                  />
              </div>
              <div title="修改">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  circle
                  @click="edit(scope.row.id)"
                />
              </div>
              <div title="删除">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  class="delete-user"
                  circle
                  @click="deleteQuestion(scope.row.id)"
                />
              </div>
              <div title="加入精选">
                <el-button
                  type="warning"
                  plain
                  icon="el-icon-check"
                  class="delete-user"
                  circle
                  @click="addQuestion(scope.row.id)"
                />
              </div>
            </div>

          </template>
          <template
            v-slot:question="scope"
          >
            <p v-html="scope.row.question"></p>
          </template>
        </my-table>
      </div>
      <!-- 单选 -->
      <el-dialog
        title="题目预览"
        :visible.sync="topicRadioVisible"
        width="70%"
      >
        <div class="topic-container">
          <div class="topic">
            <div>【题型】：{{ detail.questionType }}</div>
            <div>【编号】: {{ detail.number }}</div>
            <div>【难度】：{{ detail.difficulty }}</div>
            <div>【标签】: </div>
            <div>【学科】：{{ detail.subjectID }}</div>
            <div>【目录】: {{ detail.catalogID }}</div>
            <div>【方向】：{{ detail.direction }}</div>
          </div>
          <div class="questions-stems">
            <div class="questions-size">题干</div>
            <div
              class="questions-stem"
              v-html="detail.question"
            />
            <div class="Radio-title">
              单选题 选项：（以下选中的选项为正确答案）
            </div>
            <!-- 单选 -->
            <div
              class="Radio"
              v-show="detail.questionType !== '2'"
            >
              <div
                v-for="item in detail.options"
                :key="item.id"
              >
                <el-radio
                  v-model="radio"
                  :label="item.code"
                >
                  {{ item.title }}
                </el-radio>
              </div>
            </div>
            <div class="reference">
              <div class="reference-title">【参考答案】：</div>
              <div>
                <el-button
                  type="danger"
                  @click="videoClick"
                >
                  视频答案预览
                </el-button>
              </div>
              <div>
                <video
                  width="320"
                  height="240"
                  controls
                  autoplay
                  v-show="videoVisible"
                >
                  <source src="movie.ogg" type="video/ogg">
                  <source src="movie.mp4" type="video/mp4">
                  <source src="movie.webm" type="video/webm">
                  <object data="movie.mp4" width="320" height="240">
                    <embed width="320" height="240" src="movie.swf">
                  </object>
                </video>
              </div>
            </div>
            <div class="analysis">
              <p>
                【答案解析】：
              </p>
              <div v-html="detail.answer"/>
            </div>
            <div class="remark">【题目备注】：{{ detail.remarks }}</div>
            <div class="btn">
              <el-button
                type="primary"
                @click="topicVisible = false"
              >
                关闭
              </el-button>
            </div>
          </div>
        </div>
      </el-dialog>

      <!-- 多选 -->
      <el-dialog
        title="题目预览"
        :visible.sync="topicVisible"
        width="70%"
      >
        <div class="topic-container">
          <div class="topic">
            <div>【题型】：{{ detail.questionType }}</div>
            <div>【编号】: {{ detail.number }}</div>
            <div>【难度】：{{ detail.difficulty }}</div>
            <div>【标签】: </div>
            <div>【学科】：{{ detail.subjectID }}</div>
            <div>【目录】: {{ detail.catalogID }}</div>
            <div>【方向】：{{ detail.direction }}</div>
          </div>
          <div class="questions-stems">
            <div class="questions-size">题干</div>
            <div
              class="questions-stem"
              v-html="detail.question"
            />
            <div
              class="Radio-title"
              v-show="detail.questionType !== '2'"
            >
              单选题 选项：（以下选中的选项为正确答案）
            </div>
            <div
              class="Radio-title"
              v-show="detail.questionType !== '1'"
            >
              多选题 选项：（以下选中的选项为正确答案）
            </div>
            <!-- 单选 -->
            <div
              class="Radio"
              v-show="detail.questionType !== '2'"
            >
              <div
                v-for="item in detail.options"
                :key="item.id"
              >
                <el-radio
                  v-model="radio"
                  :label="item.code"
                >
                  {{ item.title }}
                </el-radio>
              </div>
            </div>
            <!-- 多选 -->
            <div
              class="Radio"
              v-show="detail.questionType !== '1'"
            >
              <div
                v-for="item in detail.options"
                :key="item.id"
              >
                <el-checkbox
                  :checked="item.isRight === 1 ? true : false"
                  :label="item.isRight"
                >
                  {{ item.title }}
                </el-checkbox>
              </div>
            </div>
            <div class="reference">
              <div class="reference-title">【参考答案】：</div>
              <div>
                <el-button
                  type="danger"
                  @click="videoClick"
                >
                  视频答案预览
                </el-button>
              </div>
              <div>
                <video
                  width="320"
                  height="240"
                  controls
                  autoplay
                  v-show="videoVisible"
                >
                  <source src="movie.ogg" type="video/ogg">
                  <source src="movie.mp4" type="video/mp4">
                  <source src="movie.webm" type="video/webm">
                  <object data="movie.mp4" width="320" height="240">
                    <embed width="320" height="240" src="movie.swf">
                  </object>
                </video>
              </div>
            </div>
            <div class="analysis">
              <p>
                【答案解析】：
              </p>
              <div v-html="detail.answer"/>
            </div>
            <div class="remark">【题目备注】：{{ detail.remarks }}</div>
            <div class="btn">
              <el-button
                type="primary"
                @click="topicVisible = false"
              >
                关闭
              </el-button>
            </div>
          </div>
        </div>


      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import * as questionsApi from '@/api/hmmm/questions';
import * as constants from '@/api/hmmm/constants'
import { datas, provinces, citys } from '@/api/hmmm/citys'
export default {
  name: 'questions',
  data() {
    return {
      // 城市区域函数
      district: citys,
      // 城市函数
      cityslist: provinces,
      // 城市数据
      citydatas: datas,
      // 多选题弹窗
      topicVisible: false,
      // 单选题弹窗
      topicRadioVisible: false,
      // 视频题弹窗
      videoVisible: false,
      page: 1,
      pagesize: 5,
      events: {
        'size-change': this.handleSizeChange,
        'current-change': this.handleCurrentChange,
      },
      // 单选题干反显
      radio: 'A',
      questionSearch: {
        subjectID: '',
        catalogID: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creatorID: '',
        remarks: '',
        shortName: '',
        province: '',
        city: '',
      },
      // 试题类型
      questionTypes:[
        {
          label: '单选',
          value:  1,
        },
        {
          label: '多选',
          value: 2
        },
        {
          label: '简答',
          value: 3
        }
      ],
      // 难度
      difficulties:[
        {
          label: '简单',
          value: 1,
        },
        {
          label: '中等',
          value: 2
        },
        {
          label: '困难',
          value: 3
        }
      ],
      // 标签
      direction: constants.direction,
      enterprise: {
        tags: '',
        province: '',
        city: '',
        shortName: '',
        state: '',
      },
    }
  },
  created() {
    this.uptateBasicquestionsList({
      page: this.page,
      pagesize: this.pagesize,
    });
    this.getSimpleList()
    // 管理员
    this.uptateUsersList()
    // 二级目录
    this.uptatecatalogueslist()
  },
  computed: {
    // 试题列表
    ...mapState('questions', [
      'basicquestions',
      'detail'
      ]),
    // 管理员
    ...mapState('users', [
      'usersList',
    ]),
    // 简单学科列表
    ...mapState('subjects', [
        'simpleList'
    ]),
    // 简单目录列表
    ...mapState('directorys', [
      'catalogueslist'
    ]),
  },
  methods: {
    // 试题列表
    ...mapActions('questions', [
      'uptateBasicquestionsList',
      'uptateDetail'
    ]),
    // 管理员
    ...mapActions('users', [
      'uptateUsersList',
    ]),
    // 简单学科列表
    ...mapActions('subjects', [
      'getSimpleList'
    ]),
    // 简单目录列表
    ...mapActions('directorys', [
      'uptatecatalogueslist'
    ]),
    // 跳转到试题添加页面
    addTest() {
      this.$router.push('new');
    },
    // 城市select
    changeCity(val) {
      this.district(val,this.citydatas);
      // 区域反显
      const transition = this.district(val,this.citydatas)[0];
      this.questionSearch.city = transition;
    },
    // 清除
    eliminate() {
      this.questionSearch = {}
      // this.$refs.questionSearch.resetFields();
      // 刷新列表
      this.uptateBasicquestionsList({
        page: this.page,
        pagesize: this.pagesize,
      });
    },
    // 搜索
    enterpriseSearch() {
      const params = this.questionSearch;
      for (const key in params) {
        if (params[key] === '') {
          delete params[key];
        }
      }
      this.uptateBasicquestionsList({
        page: this.page,
        pagesize: this.pagesize,
        ...params,
      });
    },
    // 预览
    async preview (id) {
      await this.uptateDetail({
        id: id,
      })
      const options = this.detail.options;
      const checkeds = options.filter(d => d.isRight === 1)
      if (checkeds.length > 1) {
        this.checked = checkeds.map(d => d.isRight)
        this.topicVisible = true;
      } else {
        this.radio = options.find(d => d.isRight === 1).code;
        this.topicRadioVisible = true;
      }
    },
    // 视频弹出
    videoClick() {
      this.videoVisible = !this.videoVisible;
    },
    // 修改
    edit(id) {
      this.$router.push({
        path: 'new',
        query: {
          id: id,
        },
      });
    },
    // 每页条数
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(val);
      this.uptateBasicquestionsList({
        page: this.page,
        pagesize: this.pagesize
      })
    },
    // 当前页数
    handleCurrentChange(val) {
      console.log('当前页数',val);
      this.page = val;
      this.uptateBasicquestionsList({
        page: this.page,
        pagesize: this.pagesize
      })
    },
    // 删除试题
    deleteQuestion(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await questionsApi.removequestions(id)
        .then(() => {
          this.$message.success('删除成功');
          this.uptateBasicquestionsList({
            page: this.page,
            pagesize: this.pagesize
          })
        })
        .catch(() => {
          this.$message.error('删除失败');
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 加入精选
    addQuestion(id) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await questionsApi.choiceAdd({
          id: id,
          choiceState: 1
        })
        .then(() => {
          this.$message.success('加入成功');
          this.uptateBasicquestionsList({
            page: this.page,
            pagesize: this.pagesize
          })
        })
        .catch(() => {
          this.$message.error('加入失败');
        })
      })
    },
    // 鼠标移入显示文字
    // popup($event) {
    //   const popup = this.$refs.popup;
    //   console.log($event);
    //   console.log(popup);
    // },
  }
}
</script>

<style scoped lang='scss'>
.dashboard-container {
  padding: 16px;

  .test-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #ffffff;
    margin-bottom: 16px;

    .size {
      color: pink;
      font-size: 12px;
      margin-left: 16px;
    }
  }

  .main {
    padding-right: 12px;
    padding-top: 12px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    background-color: #ffffff;

    .form-container {
      // grid布局
      display: grid;
      grid-template-columns: repeat(4, 25%);
      background-color: #ffffff;

      // 城市选择
      .city-select {
        // grid布局
        display: grid;
        grid-template-columns: repeat(2, 50%);
        // 中间有空隙
        grid-gap: 10px;
        padding-right: 10px;
      }

      .bnt {
        // 靠右对齐
        display: flex;
        justify-content: flex-end;
        align-items: center;
      }

      .add-user {
        float: right;
      }
    }

    .record-container {
      background-color: #f4f4f5;
      border-radius: 4px;
      padding: 8px;
      margin-left: 26px;
      font-size: 13px;
      color: #909399;
      margin-bottom: 16px;
    }

    .derive-list {
      padding-left: 26px;

      .operation {
        display: grid;
        grid-template-columns: repeat(4, 25%);
      }
    }

    .topic-container {
      // 题目预览弹出框
      .topic {
        display: grid;
        grid-template-columns: repeat(4, 25%);
        grid-gap: 20px;
        padding: 20px 0;
        border-bottom: 1px solid #9a9a9a;
      }

      // 题干容器
      .questions-stems {
        padding: 16px 0;

        // 题干
        .questions-size {
          margin-bottom: 16px;
        }

        // 单选题标题
        .Radio-title {
          padding: 16px 0;
          border-bottom: 1px solid #9a9a9a;
        }

        // 单选题选项
        .Radio {
          border-bottom: 1px solid #9a9a9a;
          div:nth-child(1) {
            padding-top: 16px;
          }
          div:nth-child(4) {
            padding-bottom: 16px;
          }
          div {
            padding: 8px 0;
          }
        }

        // 参考答案
        .reference {
          display: grid;
          grid-template-columns: repeat(2, 15%);
          // 垂直居中
          align-items: center;
          padding: 8px 0;
        }

        // 答案解析
        .analysis {
          display: flex;
          padding: 8px 0;
          border-top: 1px solid#9a9a9a;
          border-bottom: 1px solid#9a9a9a;
        }

        // 备注
        .remark {
          margin-top: 8px;
        }

        // 关闭
        .btn {
          padding: 8px 0;
          text-align: right;
        }
      }
    }


  }

}

// 复选框禁止点击
::v-deep .el-checkbox {
  pointer-events:none;
  cursor:not-allowed;
}

// 单选框禁止点击
::v-deep .el-radio {
  pointer-events:none;
  cursor:not-allowed;
}

// 分页靠右
::v-deep .el-pagination {
  text-align: right;
}

</style>
